package com.jinxin.platform.cdsideparkmgr.rpc;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jinxin.platform.base.api.annotation.*;
import com.jinxin.platform.base.api.constant.APCOSApiConstans;
import com.jinxin.platform.base.common.pojo.InvokeCfg;
import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.base.common.support.BusinessDelegate;
import com.jinxin.platform.cdsideparkmgr.bean.bo.OptionItem;
import com.jinxin.platform.cdsideparkmgr.common.Constant;
import com.jinxin.platform.cdsideparkmgr.common.SysOrgInfo;
import com.jinxin.platform.cdsideparkmgr.common.SysUserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.alibaba.fastjson.JSON.toJSONString;

/**
 * @description: (描述...)
 * @return:
 * @author: Linn
 * @time: 2020/05/18
 */
@Component
@Slf4j
@NeedInf(moduleKey = Constant.MODULE_KEY, className = Constant.MODULE_CLASS_NAME)
public class CdsideparkmgrRpc {
    @Autowired
    private BusinessDelegate businessDelegate;

    // 推送公众号消息
    @APCOSApiOperation(value = "pushPaMsg", description = "推送公众号消息", infDict = "推送公众号消息",
            reqDemo = "{\"templateId\":\"模板ID\",\"openId\":\"openId\",\"msgContent\":\"json格式的消息内容(根据模板定)\"}", respDemo = "{}",
            infType = APCOSApiConstans.INF_TYPE_OTHER)
    @APCOSApiRequests({
            @APCOSApiRequest(name = "templateId", value = "模板id", dict = "模板id",matchType = APCOSApiConstans.MATCH_TYPE_EQ,
                    fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "模板id"),
            @APCOSApiRequest(name = "openId", value = "接收者openid", dict = "接收者openid",matchType = APCOSApiConstans.MATCH_TYPE_EQ,
                    fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "接收者openid"),
            @APCOSApiRequest(name = "msgContent", value = "推送消息内容(json格式)", dict = "推送消息内容(json格式)",matchType = APCOSApiConstans.MATCH_TYPE_EQ,
                    fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "2000",remark = "推送消息内容(json格式)"),
    })
    public void pushPaMsg(String templateId,String openId,String msgContent) {
        InvokeCfg cfg = new InvokeCfg();
        cfg.setModuleKey(Constant.MODULE_KEY);
        cfg.setClassName(Constant.MODULE_CLASS_NAME);
        cfg.setFunName("pushPaMsg");
        HashMap<String, String> map = new HashMap<>();
        map.put("templateId",templateId);
        map.put("openId",openId);
        map.put("msgContent",msgContent);
        cfg.setArgs(map);
        businessDelegate.invoke(cfg);
    }

    /**
     * 获取当前用户对象
     *
     * @return SysUser
     */
    @APCOSApiOperation(value = "getCurrentUser", description = "获取当前登录者", infDict = "获取当前登录者",
            reqDemo = "{}", respDemo = "{\"userId\":\"123\",\"userName\":\"张三\",\"userAccount\":\"13812345678\"}",
            infType = APCOSApiConstans.INF_TYPE_OTHER)
    @APCOSApiRequests({})
    @APCOSApiResponses({
            @APCOSApiResponse(name = "userAccount", value = "登录账户", dict = "登录账户",required = true,
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "登录账户"),
            @APCOSApiResponse(name = "userId", value = "用户ID", dict = "用户ID",required = true,
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "用户ID"),
            @APCOSApiResponse(name = "userName", value = "用户姓名", dict = "用户姓名",required = true,
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "用户姓名"),
    })
    public SysUserVO getCurrentUser() {
        JsonResult result = getJsonResult(Constant.MODULE_KEY,"getCurrentUser");
        if (result.getSuccess()) {
            if (result.getData() == null) {
                return null;
            }
            log.info("getCdsideparkmgrCurrentUser:" + result.getData());
            return JSON.parseObject(toJSONString(result.getData()), SysUserVO.class);
        }
//        else {
//            SysUserVO userVO = new SysUserVO();
//            userVO.setUserId("2");
//            userVO.setUserAccount("二狗子");
//            userVO.setUserName("admin");
//            return userVO;
//        }
        return null;
    }

    /**
     * 获取当前用户id获取所在组织
     *
     * @return SysUser
     */
    @APCOSApiOperation(value = "getCurrentOrg", description = "根据用户ID所在的组织", infDict = "根据用户ID所在的组织",
            reqDemo = "{\"userId\":\"123\"}", respDemo = "{\"orgId\":\"123\"}",
            infType = APCOSApiConstans.INF_TYPE_OTHER)
    @APCOSApiRequests({
            @APCOSApiRequest(name = "userId", value = "用户ID", dict = "用户ID",matchType = APCOSApiConstans.MATCH_TYPE_EQ,
                    fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "用户ID"),
    })
    @APCOSApiResponses({
            @APCOSApiResponse(name = "orgId", value = "组织ID", dict = "组织ID",required = true,
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "组织ID"),
    })
    public SysOrgInfo getCurrentOrg() {
        SysUserVO currentUser = getCurrentUser();
        if(currentUser == null){return null; }
        InvokeCfg cfg = new InvokeCfg();
        cfg.setModuleKey(Constant.MODULE_KEY);
        cfg.setClassName(Constant.MODULE_CLASS_NAME);
        cfg.setFunName("getCurrentOrg");
        HashMap<String, String> map = new HashMap<>();
        map.put("userId",currentUser.getUserId());
        cfg.setArgs(map);
        JsonResult result = businessDelegate.invoke(cfg);
        if (result.getSuccess()) {
            if (result.getData() == null) {
                return null;
            }
            log.info("getCurrentOrg:" + result.getData());
            return JSON.parseObject(toJSONString(result.getData()), SysOrgInfo.class);
        }
//        else {
//            SysOrgInfo userVO = new SysOrgInfo();
//            userVO.setOrgId("1");
//            userVO.setOrgName("一个牛逼的公司");
//            return userVO;
//        }
        return null;
    }

    private JsonResult getJsonResult(String keyName,String funName) {
        InvokeCfg cfg = new InvokeCfg();
        cfg.setModuleKey(keyName);
        cfg.setClassName(Constant.MODULE_CLASS_NAME);
        cfg.setFunName(funName);
        return businessDelegate.invoke(cfg);
    }

    /**
     * 查询角色列表
     * @return
     */
    @APCOSApiOperation(value = "loadRoleList", description = "获取组织下的角色列表", infDict = "获取组织下的角色列表",
            reqDemo = "{\"orgId\":\"123\"}", respDemo = "{\n" +
            "  \"data\": [\n" +
            "    {\n" +
            "      \"id\": \"角色ID\",\n" +
            "      \"name\": \"角色名称\"\n" +
            "    }\n" +
            "  ]\n" +
            "} ",
            infType = APCOSApiConstans.INF_TYPE_OTHER)
    @APCOSApiRequests({
            @APCOSApiRequest(name = "orgId", value = "组织ID", dict = "组织ID",matchType = APCOSApiConstans.MATCH_TYPE_EQ,
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,required = true,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "组织ID"),
    })
    @APCOSApiResponses({
            @APCOSApiResponse(name = "data", value = "返回集合", dict = "返回集合",required = true,
                    fieldType = APCOSApiConstans.FIELD_TYPE_GN,keyType = APCOSApiConstans.KEY_TYPE_LIST,
                    dataType = APCOSApiConstans.DATA_TYPE_ARR,remark = "返回集合"),
            @APCOSApiResponse(name = "data.id", value = "角色ID", dict = "角色ID",required = true,
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "角色ID"),
            @APCOSApiResponse(name = "data.name", value = "角色名称", dict = "角色名称",required = true,
                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "角色名称"),
    })
    public JsonResult loadRoleList() {
        SysOrgInfo currentOrg = getCurrentOrg();
        if(currentOrg == null){
            return null;
        }
        InvokeCfg cfg = new InvokeCfg();
        cfg.setModuleKey(Constant.MODULE_KEY);
        cfg.setClassName(Constant.MODULE_CLASS_NAME);
        cfg.setFunName("loadRoleList");
        HashMap<String, String> map = new HashMap<>();
        map.put("orgId",currentOrg.getOrgId());
        cfg.setArgs(map);
        JsonResult result = businessDelegate.invoke(cfg);
        log.info("loadRoleList =====>" + result.toString());
        if(result.getSuccess()){
            return result;
        }
//        else {
//            List<OptionItem> list = new ArrayList<>();
//            OptionItem item1 = new OptionItem("1","角色1");
//            OptionItem item2 = new OptionItem("2","角色2");
//            list.add(item1);
//            list.add(item2);
//            return JsonResult.ok(list);
//        }
        return null;
    }

    public JsonResult loadUserList() {
        SysOrgInfo currentOrg = getCurrentOrg();
        if(currentOrg == null){
            return null;
        }
        List<OptionItem> users = getUsersByOrgId(currentOrg.getOrgId());
        return JsonResult.ok(users);
    }

    /**
     * 字典类型获取字典项
     * @param dictType 字典类型
     * @return
     */
//    @APCOSApiOperation(value = "getDictItems", description = "根据字典类型查询字典项", infDict = "根据字典类型查询字典项",
//            reqDemo = "{\"dictType\":\"业务类型\"}", respDemo = " {}",
//            infType = APCOSApiConstans.INF_TYPE_OTHER)
//    @APCOSApiRequests({
//            @APCOSApiRequest(name = "dictType", value = "字典类型", dict = "字典类型",matchType = APCOSApiConstans.MATCH_TYPE_EQ,
//                    keyType = APCOSApiConstans.KEY_TYPE_STR,fieldType = APCOSApiConstans.FIELD_TYPE_GN,required = true,
//                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "字典类型"),
//    })
//    public JsonResult getDictItems(String dictType) {
//        InvokeCfg cfg = new InvokeCfg();
//        cfg.setModuleKey(Constant.MODULE_KEY);
//        cfg.setClassName(Constant.MODULE_CLASS_NAME);
//        cfg.setFunName("getDictItems");
//        HashMap<String, String> map = new HashMap<>();
//        map.put("dictType", dictType);
//        cfg.setArgs(map);
//        JsonResult result = businessDelegate.invoke(cfg);
//        log.info("getDictItems =====>" + result.toString());
//        if(result.getSuccess()){
//            return result;
//        }
//        else {
//            List<DictItem> list = new ArrayList<>();
//            DictItem item1 = new DictItem("1","工程车辆");
//            DictItem item2 = new DictItem("2","社会车辆");
//            list.add(item1);
//            list.add(item2);
//            return JsonResult.ok(list);
//        }
//        return null;
//    }
    /**
     * 查询组织下的用户
     *
     * @return SysUser
     */
    @APCOSApiOperation(value = "getUsersByOrgId", description = "按条件获取用户", infDict = "按条件获取用户",
            reqDemo = "{\"orgId\":\"123\"}", respDemo = "{\n" +
            "  \"data\": [\n" +
            "    {\n" +
            "      \"id\": \"用户ID\",\n" +
            "      \"name\": \"用户姓名\",\n" +
            "      \"userAccount\":\"登录账户\",\n" +
            "      \"userPhone\":\"用户电话\"\n" +
            "    }\n" +
            "  ]\n" +
            "} ",
            infType = APCOSApiConstans.INF_TYPE_OTHER)
    @APCOSApiRequests({
            @APCOSApiRequest(name = "orgId", value = "组织ID", dict = "组织ID",matchType = APCOSApiConstans.MATCH_TYPE_EQ,
                    fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "组织ID"),
    })
    @APCOSApiResponses({
            @APCOSApiResponse(name = "data", value = "返回集合", dict = "返回集合",required = true,
                    fieldType = APCOSApiConstans.FIELD_TYPE_GN,keyType = APCOSApiConstans.KEY_TYPE_LIST,
                    dataType = APCOSApiConstans.DATA_TYPE_ARR,remark = "返回集合"),
            @APCOSApiResponse(name = "data.userAccount", value = "登录账户", dict = "登录账户",required = true,
                    fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "登录账户"),
            @APCOSApiResponse(name = "data.id", value = "用户ID", dict = "用户ID",required = true,
                    fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "用户ID"),
            @APCOSApiResponse(name = "data.name", value = "用户姓名", dict = "用户姓名",required = true,
                    fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "用户姓名"),
            @APCOSApiResponse(name = "data.userPhone", value = "用户电话", dict = "用户电话",
                    fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50",remark = "用户电话"),
    })
    public List<OptionItem> getUsersByOrgId(String orgId) {
        InvokeCfg cfg = new InvokeCfg();
        cfg.setModuleKey(Constant.MODULE_KEY);
        cfg.setClassName(Constant.MODULE_CLASS_NAME);
        cfg.setFunName("getUsersByOrgId");
        HashMap<String, String> map = new HashMap<>();
        map.put("orgId",orgId);
        cfg.setArgs(map);
        JsonResult result = businessDelegate.invoke(cfg);
        if (result.getSuccess()) {
            if (result.getData() == null) {
                return null;
            }
            log.info("getUsersByOrgId:" + result.getData());
            JSONObject jsonObj = JSONObject.parseObject(result.getData().toString());
            return JSON.parseArray(jsonObj.get("data").toString(), OptionItem.class);
        }
//        else {
//            List<OptionItem> list = new ArrayList<>();
//            OptionItem item1 = new OptionItem("1","王麻子");
//            OptionItem item2 = new OptionItem("2","二狗子");
//            list.add(item1);
//            list.add(item2);
//            return list;
//        }
        return null;
    }

}
